﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class ctlInsurance : UserControl
    {
        /// <summary>
        /// 当前查询条件
        /// </summary>
        string curConSql = string.Empty;
        DataSet curDataSet = new DataSet();

        public ctlInsurance()
        {
            InitializeComponent();
            Init();

        }

        /// <summary>
        /// 刷新界面
        /// </summary>
        public void RefreshUI()
        {
            this.tscboCond.SelectedIndex = 0;
            this.curConSql = string.Empty;
            Bind(1, false);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        void Init()
        {
            Common.SetGridViewStyle(this.dataGridView1);
            this.ctlPager1.PageSize = MyGlobal.PageSize;
            this.tscboCond.SelectedIndex = 0;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnAdd_Click(object sender, EventArgs e)
        {
            frmInsurance fi = new frmInsurance(ActionType.New, string.Empty);
            if (fi.ShowDialog() == DialogResult.OK)
            {
                Bind(1, false);
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnEdit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count != 0)
            {
                string id = this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString();
                frmInsurance fi = new frmInsurance(ActionType.Edit, id);
                if (fi.ShowDialog() == DialogResult.OK)
                {
                    Bind(1, false);
                }
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="isPageEvent"></param>
        private void Bind(int pageIndex, bool isPageEvent)
        {
            InsuranceForm insuranceForm = new InsuranceForm();
            DataTable dt = insuranceForm.Query(this.curConSql);

            //计算分页信息
            int startPos = (pageIndex - 1) * this.ctlPager1.PageSize;
            int count = this.ctlPager1.PageSize;
            if ((pageIndex - 1) * ctlPager1.PageSize + ctlPager1.PageSize > dt.Rows.Count)
            {
                count = dt.Rows.Count - startPos;
            }
            int total = dt.Rows.Count;

            //筛选分页数据
            Common.GetRange(dt, startPos, count);
            if (!isPageEvent)
            {
                this.ctlPager1.Initialize(total);
            }
            this.dataGridView1.DataSource = dt;
            curDataSet.Tables.Clear();
            curDataSet.Tables.Add(dt.Copy());
            this.dataGridView1.Columns[0].Width = 60;

            this.dataGridView1.Columns["编号"].Visible = false;
            this.dataGridView1.Columns["序号"].Width = 60;
            this.dataGridView1.Columns["交强险保单号"].Width = 110;
            this.dataGridView1.Columns["商业险保单号"].Width = 110;
            this.dataGridView1.Columns["被保险人"].Width = 100;
            this.dataGridView1.Columns["交强险参保日期"].Width = 120;
            this.dataGridView1.Columns["交强险到期日期"].Width = 120;
            this.dataGridView1.Columns["商业险参保日期"].Width = 120;
            this.dataGridView1.Columns["商业险到期日期"].Width = 120;
            this.dataGridView1.Columns["联系电话"].Width = 100;
            this.dataGridView1.Columns["车辆号牌"].Width = 100;
            this.dataGridView1.Columns["车辆类型"].Width = 100;
            this.dataGridView1.Columns["车架号"].Width = 100;
            this.dataGridView1.Columns["金额"].Width = 80;

            this.dataGridView1.Columns["交强险保单号"].ReadOnly = true;
            this.dataGridView1.Columns["商业险保单号"].ReadOnly = true;
            this.dataGridView1.Columns["被保险人"].ReadOnly = true;
            this.dataGridView1.Columns["交强险参保日期"].ReadOnly = true;
            this.dataGridView1.Columns["交强险到期日期"].ReadOnly = true;
            this.dataGridView1.Columns["商业险参保日期"].ReadOnly = true;
            this.dataGridView1.Columns["商业险到期日期"].ReadOnly = true;
            this.dataGridView1.Columns["联系电话"].ReadOnly = true;
            this.dataGridView1.Columns["车辆号牌"].ReadOnly = true;
            this.dataGridView1.Columns["车辆类型"].ReadOnly = true;
            this.dataGridView1.Columns["车架号"].ReadOnly = true;
            this.dataGridView1.Columns["金额"].ReadOnly = true;

            this.dataGridView1.Columns["交强险参保日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["交强险到期日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["商业险参保日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["商业险到期日期"].DefaultCellStyle.Format = "yyyy-MM-dd";
            this.dataGridView1.Columns["报户日期"].DefaultCellStyle.Format = "yyyy-MM-dd";

            this.dataGridView1.Columns["核定"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.dataGridView1.Columns["单位"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dataGridView1.Columns["金额"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.dataGridView1.Columns["金额"].DefaultCellStyle.Format = "0.00";

            this.chkSelectAll.Checked = false;

        }

        private void ctlInsurance_Load(object sender, EventArgs e)
        {
            Bind(1, false);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count != 0)
            {
                if (MessageHelper.ShowYesNoChoice("确定删除‘" + this.dataGridView1.SelectedRows[0].Cells["车辆号牌"].Value.ToString() + "’的保单信息吗？") == DialogResult.Yes)
                {
                    try
                    {
                        InsuranceDetail insuranceDetail = new InsuranceDetail();
                        insuranceDetail.DeleteWithCondition(" formID='" + this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString() + "'");
                        InsuranceForm insuranceForm = new InsuranceForm();
                        insuranceForm.Delete(this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString());
                        Bind(1, false);
                    }
                    catch
                    {
                        MessageHelper.ShowFail("删除失败！");
                    }

                }
            }
        }

        /// <summary>
        /// 清除查询条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnClearCon_Click(object sender, EventArgs e)
        {
            this.tscboCond.SelectedIndex = 0;
            this.tstxtQueryValue.Text = string.Empty;
            this.curConSql = string.Empty;
            Bind(1, false);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tssbtnQuery_ButtonClick(object sender, EventArgs e)
        {
            string con = this.tscboCond.SelectedItem.ToString();
            string value = this.tstxtQueryValue.Text.Trim().Replace("'", "''");
            string sqlcon = string.Empty;
            switch (con)
            {
                case "车辆号牌":
                    sqlcon = " CarNumber  like '%" + value + "%'";
                    break;
                case "被保险人":
                    sqlcon = " OwnerName like '%" + value + "%'";
                    break;
                case "联系电话":
                    sqlcon = " Telephone like '%" + value + "%'";
                    break;
                case "保单号":
                    sqlcon = " InsuranceNumber like '%" + value + "%'";
                    break;
            }
            this.curConSql = sqlcon;
            Bind(1, false);
        }

        /// <summary>
        /// 按日期查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiQueryByCheckDate_Click(object sender, EventArgs e)
        {
            frmSearchByDateForInsurance searchByDate = new frmSearchByDateForInsurance();
            if (searchByDate.ShowDialog() == DialogResult.OK)
            {
                this.curConSql = searchByDate.GetSqlStr();
                Bind(1, false);
            }
        }

        /// <summary>
        /// 单元格双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > 0)
            {
                if (this.dataGridView1.SelectedRows.Count != 0)
                {
                    string id = this.dataGridView1.SelectedRows[0].Cells["编号"].Value.ToString();
                    frmInsurance fi = new frmInsurance(ActionType.Edit, id);
                    if (fi.ShowDialog() == DialogResult.OK)
                    {
                        Bind(1, false);
                    }
                }
            }
        }

        /// <summary>
        /// 全选和反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSelectAll.Checked)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.EndEdit();
                    DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                    checkEditCell.Value = true;
                }
            }
            else
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.EndEdit();
                    DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                    checkEditCell.Value = false;
                }
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnPrint_Click(object sender, EventArgs e)
        {
            RDLCPrint.ReportViewer frmRPT = new RDLCPrint.ReportViewer();
            frmRPT.MainDataSet = curDataSet;
            frmRPT.ReportName = "Test";
            frmRPT.MainDataSourceName = "dbDataSet_InsuranceFormForReport";
            frmRPT.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Report\Insurance.rdlc";
            frmRPT.ShowDialog();
        }

        /// <summary>
        /// 获取用户选择的数据
        /// </summary>
        /// <returns></returns>
        private string GetSelectRecord()
        {
            string ids = string.Empty;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.EndEdit();
                DataGridViewCheckBoxCell checkEditCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["chkSelect"];
                Boolean flag = Convert.ToBoolean(checkEditCell.Value);
                if (flag)
                {
                    ids += "'" + this.dataGridView1.Rows[i].Cells["编号"].Value.ToString() + "',";
                }
            }
            return ids.TrimEnd(',');
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            string rids = GetSelectRecord();
            if (string.IsNullOrEmpty(rids))
            {
                MessageHelper.ShowInfo("请选择要发送短信的客户！");
                return;
            }
            frmSendMsg msg = new frmSendMsg(MsgType.Insurance, rids);
            msg.ShowDialog();
        }

        /// <summary>
        /// 分页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void ctlPager1_PageChanged(object sender, Controls.PagerArgs args)
        {
            Bind(args.PageIndex, true);
        }
    }
}
